package example.hellosecurity.config;

/*
* 实现自定义403页面
* */

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest,
                       HttpServletResponse httpServletResponse,
                       AccessDeniedException e) throws IOException, ServletException {


//        设置响应状态
        httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);

//        设置响应类型
        httpServletResponse.setContentType("text/html;charset=UTF-8");

//        响应输出
        httpServletResponse.getWriter().println("<html><body><h1>你没有权限</h1></body></html>");


    }
}
